﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Attendences.Framework.BusinessLogic;
using Attendences.Framework.Entity;

public partial class Page_UserAttendDetail : BaseAuthenticationPage
{
    public int AttendID
    {
        get
        {
            if (Request.QueryString["ID"] != null)
                return int.Parse(Request.QueryString["ID"]);
            return 0;
        }
    }

    protected DataTable AttendenceDateUser
    {
        get
        {
            if (ViewState["AttendenceDateUser"] == null)
                ViewState["AttendenceDateUser"] = new DataTable();
            return ViewState["AttendenceDateUser"] as DataTable;
        }
        set
        {
            ViewState["AttendenceDateUser"] = value;
        }
    }

    protected DataTable AttendItemList
    {
        get
        {
            if (ViewState["AttendItemList"] == null)
                ViewState["AttendItemList"] = new DataTable();
            return ViewState["AttendItemList"] as DataTable;
        }
        set
        {
            ViewState["AttendItemList"] = value;
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "值班详细清单";
        this.UseTabber = true;
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
        ClientScript.RegisterStartupScript(this.GetType(), "Finish", "<script>parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>");

    }

    private void BindData()
    {
        Attendence attendence = new Attendence(AttendID);
        attendence.Load();

        ltTitle.Text = "值班表单";
        ltDescription.Text = attendence.Description;
        ltCreateTime.Text = attendence.CreateTime.ToString("yyyy年MM月dd日 HH:mm:ss");
        ltName.Text = attendence.Name;
        ltPlace.Text = attendence.Place;

        ltTitle2.Text = "值班详情";
        ltName2.Text = attendence.Name;
        ltPlace2.Text = attendence.Place;
        //ltCreateTime2.Text = attendence.CreateTime.ToString("yy-MM-dd");

        //从当前选定的表Attendence的ID通过AttendenceID返回当前选定的AttendenceItem表中的内容
        //从当前选定的表Attendence的ID通过AttendenceID返回当前选定的AttendenceItem表中的内容
        DataTable dt = BLAttendences.GetAttendenceItem(string.Format("{0} = {1}", AttendenceItem.SQLCOL_ATTENDENCEID, AttendID));
        AttendItemList = dt.Clone();
        DataRow[] drs = dt.Select("1=1", "AttendDate");
        foreach (DataRow dr in drs)
        {
            AttendItemList.ImportRow(dr);
        }
        rptItemList.DataSource = AttendItemList;
        rptItemList.DataBind();
        rptItemList.DataSource = AttendItemList;
        rptItemList.DataBind();

        foreach (RepeaterItem ri in rptItemList.Items)
        {
            DataRow drItem = AttendItemList.Rows[ri.ItemIndex];
            Guid id = new Guid(drItem[AttendenceItem.SQLCOL_ID].ToString());

            AttendenceDate ad = new AttendenceDate();
            ad.AttendenceItemID = id;

            Label labUser = ri.FindControl("labUser") as Label;
            string lab = "";

            AttendenceDateUser = BLAttendences.GetAttendenceDateUser(string.Format("{0}={1} and {2}=N'{3}' order by OrderNum", AttendenceDate.SQLCOL_ATTENDENCEITEMID, id, AttendenceDate.SQLCOL_USERID, CurrentUser.ID.ToString()));

            foreach (DataRow ar in AttendenceDateUser.Rows)
            {
                if (lab == string.Empty)
                {
                    lab = ar["UserDisplayName"].ToString();
                }
                else
                {
                    lab = lab + ";" + ar["UserDisplayName"].ToString();
                }
            }
            if (lab == string.Empty)
            {
                lab = "未选定";
            }
            labUser.Text = lab;
        }
    }

    protected void rptItemList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label labUser = e.Item.FindControl("labUser") as Label;
    }
}

